package cn.edu.scau.cmi.huangmaoqin.springHibernate.clientController;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.JOptionPane;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.edu.scau.cmi.huangmaoqin.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.huangmaoqin.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.huangmaoqin.hibernate.domain.Student;
import cn.edu.scau.cmi.huangmaoqin.hibernate.domain.Teacher;
import javafx.event.ActionEvent;

import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class SpringHibernateGUIController {
	@FXML
	private Button listStudent;
	@FXML
	private Button listTeacher;
	@FXML
	private TextArea showText;
	@FXML
	private Button addStudent;
	@FXML
	private Button addTeacher;
	@FXML
	private TextField idText;
	@FXML
	private TextField idText1;
	@FXML
	private TextField nameText;

	ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "springApplicationContext.xml" });
	StudentDAO studentDAO = (StudentDAO) context.getBean("StudentDAO");
	TeacherDAO teacherDAO = (TeacherDAO) context.getBean("TeacherDAO");
	@FXML
	public void btn_Click(ActionEvent event) {
		Button btn=(Button)event.getSource();
		if(btn==addStudent) {
			if(idText.getText().length()==0||nameText.getText().length()==0) {
				JOptionPane.showMessageDialog(null, "请输入必要的信息（id和姓名）");
				return;
			}
			Long id=Long.parseLong(idText.getText());
			String name=nameText.getText();
			Long tid=Long.parseLong(idText1.getText());
			Teacher tutor=teacherDAO.findById(tid);
			Student student=new Student(id, name, tutor);
			studentDAO.save(student);
			listS();
		}else if(btn==addTeacher) {
			if(idText.getText().length()==0||nameText.getText().length()==0) {
				JOptionPane.showMessageDialog(null, "请输入必要的信息（id和姓名）");
				return;
			}
			Long id=Long.parseLong(idText.getText());
			String name=nameText.getText();
			String[] idString=idText1.getText().split(",|，");
			Set<Student> students=new HashSet<Student>();
			for(int i=0;i<idString.length;i++) {
				Long sid=Long.parseLong(idString[i]);
				students.add(studentDAO.findById(sid));
			}
			Teacher teacher=new Teacher(id, name);
			teacherDAO.save(teacher, students);
			listT();
		}else if(btn==listStudent) {
			listS();
		}else if(btn==listTeacher) {
			listT();
		}
	}
	private void listS() {
		showText.clear();
		List students = studentDAO.findAll();
		for (Object object : students) {
			Student student = (Student) object;
			if (student.getTeacher() != null)
				showText.appendText("ID为" + student.getId() + "，名字为" + student.getName() + "的学生，其老师ID为："
						+ student.getTeacher().getId() + "，名字为：" + student.getTeacher().getName());
			else
				showText.appendText("ID为" + student.getId() + "，名字为" + student.getName() + "的学生，无指导老师");
			showText.appendText("\n");
		}
	}
	private void listT() {
		showText.clear();
		List teachers = teacherDAO.findAll();
		for (Object object : teachers) {
			Teacher teacher = (Teacher) object;
			showText.appendText("ID为" + teacher.getId() + "，名字为" + teacher.getName() + "的老师，");
			if (teacher.getStudents().size() > 0) {
				showText.appendText("指导的学生有：");
				for (Object student : teacher.getStudents()) {
					showText.appendText(((Student) student).getId() + "-" + ((Student) student).getName() + " ; ");
				}
			} else {
				showText.appendText("没有指导学生;");
			}
			showText.appendText("\n");
		}
	}
}
